package resp

import (
	"bytes"
	"strconv"
)

const (
	Array  FrameFlag = '*'
	Error            = '-'
	Line             = '+'
	Multi            = '$'
	Number           = ':'
)

var NewLine = []byte("\r\n")

type FrameFlag byte

func QuoteBytes(data []byte) string {
	return strconv.Quote(string(data))
}

// 找出夹在标记和换行之间的整数，有可能是-1
func GetNum(data []byte, idx int) (int, int) {
	newIdx := idx + bytes.Index(data[idx:], NewLine)
	if num, err := strconv.Atoi(string(data[idx+1 : newIdx])); err == nil {
		return num, newIdx + 2
	}
	return 0, -1
}

func NewSpliterResp2() *SplitMatcher {
	sc := NewSplitCreator([]byte("*"), NewLine)
	sc.CheckSize = 1
	sc.CheckMore = func(data []byte) (bool, int) {
		if len(data) == 0 {
			return false, 0
		}
		if data[0] == '-' || data[0] >= '0' && data[0] <= '9' {
			return true, 1
		}
		return false, 0
	}
	return NewSplitMatcher(sc.GetSplit())
}
